//
//  CTPPhotoSelectorCell.m
//  照片选择器
//
//  Created by 檀志文 on 16/5/19.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

#import "CTPPhotoSelectorCell.h"
#import <Masonry.h>

@interface CTPPhotoSelectorCell()

//加号按钮
@property (nonatomic, strong) UIButton *addButton;
//删除按钮
@property (nonatomic, strong) UIButton *delButton;

@end

@implementation CTPPhotoSelectorCell

#pragma mark -初始化设置

// 从storyboard中加载
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self= [super initWithCoder:aDecoder]) {
        
    }
    return self;
}

// 代码创建
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        [self prepareUI];
    }
    return self;
}

-(void)prepareUI{
//    添加控件
    [self.contentView addSubview:self.addButton];
    [self.contentView addSubview:self.delButton];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
//    添加约束
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    [self.delButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.equalTo(self.contentView);
    }];
}

#pragma mark 代理方法

-(void)didClickAddButton{
        if ([self.delegate respondsToSelector:@selector(photoSelectorCellDidClickAddButton:)]) {
            [self.delegate photoSelectorCellDidClickAddButton:self];
        }
}
-(void)didClickDeleteButton{
        if ([self.delegate respondsToSelector:@selector(photoSelectorCellDidClickDeleteButton:)]) {
            [self.delegate photoSelectorCellDidClickDeleteButton:self];
        }
}


#pragma mark 设置加号按钮
-(void) setupAddButton{
    [self.addButton setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
    // 隐藏删除按钮
    self.delButton.hidden = YES;
}

#pragma mark Setter Getter方法
- (void)setImage:(UIImage *)image{
    _image = image;
    // 将图片设置给加号按钮
    [self.addButton setImage:_image forState:UIControlStateNormal];
    // 显示删除按钮
    self.delButton.hidden = NO;
}

#pragma mark 懒加载
- (UIButton *)addButton{
    if (_addButton == nil) {
        UIButton *button = [[UIButton alloc]init];
        [button setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
//      设置按钮图片的填充模式
        button.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [button addTarget:self action:@selector(didClickAddButton) forControlEvents:UIControlEventTouchUpInside];
        _addButton= button;
    }
    return _addButton;
}

- (UIButton *)delButton{
    if (_delButton == nil) {
        UIButton *button = [[UIButton alloc]init];
        [button setImage:[UIImage imageNamed:@"compose_photo_close"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(didClickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
        _delButton= button;
    }
    return _delButton;
}

@end
